﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Engine.Graphics;
using Microsoft.Xna.Framework.Graphics;
using Engine;
using System.Collections.ObjectModel;
using Microsoft.Xna.Framework;
using Estadio.GameObjects.Helpers;

namespace Estadio
{
    public class FootCourt : ModelEntity
    {
        private List<ILocator> locators;
        public ReadOnlyCollection<ILocator> Locators { get; private set; }

        private List<Handle> handles;

        public FootCourt(Entity parent, Model model)
            : base(parent, model)
        {
            handles = new List<Handle>();
            locators = new List<ILocator>();
            Locators = new ReadOnlyCollection<ILocator>(locators);

            PrepareCourt();
        }

        /// <summary>
        /// Prepares the handles and locators relying on the model meshes names.
        /// </summary>
        private void PrepareCourt()
        {
            var allLocators = from mesh in meshEntities
                              where mesh.Mesh.Name.StartsWith("mesh_Locators_")
                              select new { Mesh = mesh, Name = mesh.Mesh.Name.TrimStart("mesh_Locators_".ToCharArray()) };

            var allHandles = from mesh in meshEntities
                             where mesh.Mesh.Name.StartsWith("mesh_Handle_")
                             select new { Handle = mesh, Name = mesh.Mesh.Name.TrimStart("mesh_Handle_".ToCharArray()) };


            var teamed = from handle in allHandles
                         from locator in allLocators
                         where locator.Name[1] == handle.Name[1] && locator.Name[3] == handle.Name[3]
                         select new
                         {
                             Handle = handle.Handle,
                             Locator = locator,
                             Name = handle.Name,
                         };

            var handleWalls = from mesh in meshEntities
                              where mesh.Mesh.Name.StartsWith("mesh_courtRightWall") || mesh.Mesh.Name.StartsWith("mesh_courtLeftWall")
                              select mesh;

            foreach (var wall in handleWalls)
            {
                //wall.Visible = false;
            }

            var knobs = from mesh in meshEntities
                        where mesh.Mesh.Name.StartsWith("mesh_Knob_")
                        select new { Entity = mesh, Name = mesh.Mesh.Name.TrimStart("mesh_Knob_".ToArray()) };

            var limits = from mesh in meshEntities
                         where mesh.Mesh.Name.StartsWith("mesh_HandleLimit_")
                         select new { Entity = mesh, Name = mesh.Mesh.Name.TrimStart("mesh_HandleLimit_".ToArray()) };
            

            
            // Create a handle locator (teamed).
            foreach (var team in teamed)
            {
                HandleLocator loc = new HandleLocator(team.Locator.Mesh, team.Handle);
                locators.Add(loc);
            }

            // Make each handle keyboard controllable.
            foreach (var handle in allHandles)
            {
                Handle h = null;
                Entity knob = null;
                Entity limit = null;
                foreach (var k in knobs)
                {
                    if (handle.Name == k.Name)
                    {
                        knob = k.Entity;
                        break;
                    }
                }

                foreach (var l in limits)
                {
                    if (handle.Name == l.Name)
                    {
                        limit = l.Entity;
                    }
                }

                if (handle.Name.StartsWith("T1"))
                {
                    KeyboardHandleController khc = new KeyboardHandleController();
                    //handle.Handle.Visible = false;
                    h = new Handle(handle.Handle, knob, limit, khc, handleWalls.ToArray());
                }
                else
                {
                    DummyHandleController dhc = new DummyHandleController();
                    //handle.Handle.Visible = false;
                    h = new Handle(handle.Handle, knob, limit, dhc, handleWalls.ToArray());
                }
                handles.Add(h);
            }
        }


        public override void Update(GameTime gameTime)
        {
            float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

            foreach (var handle in handles)
            {
                handle.Update(gameTime);
            }
            base.Update(gameTime);
        }

        //public override void Draw(GameTime gameTime)
        //{
        //    foreach (var handle in handles)
        //    {
        //        handle.Draw(gameTime);
        //    }
        //    base.Draw(gameTime);
        //}


    }


}
